#ifndef __RING_BUFFER_H__
#define __RING_BUFFER_H__

#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include <pthread.h>
#include <errno.h>

#define EMPTY 0
#define OVERFLOW -1
#define BUSY -2;

struct ringbuffer {
    char *buff;
    char *front;
    char *trail;

    int buff_size;
    pthread_mutex_t bufflock;
    int data_size;

    int (*reset)(struct ringbuffer *ring);
    int (*read_pesudo)(struct ringbuffer *ring, char *buff, int size);
    int (*read_out)(struct ringbuffer *ring, char *buff, int size);
    int (*write_in)(struct ringbuffer *ring, char *buff, int size);
    int (*getdatalen)(struct ringbuffer *ring);
    void (*lock)(struct ringbuffer *ring);
    void (*unlock)(struct ringbuffer *ring);
    bool (*isbusy)(struct ringbuffer *ring);
};

struct ringbuffer* ringbuffer_create(int size);
void ringbuffer_free(struct ringbuffer *);

int ring_readout(struct ringbuffer *ring, char *buff, int size);
int ring_readpesudo(struct ringbuffer *ring, char *buff, int size);
int ring_writein(struct ringbuffer *ring, char *buff, int size);
int ring_reset(struct ringbuffer *ring);
int ring_getdatalen(struct ringbuffer *ring);

int ring_dump(struct ringbuffer *ring, int size);

#endif
